package com.lvyq.util;

import com.lvyq.model.apiinfo.ApiInfo;
import com.lvyq.model.weater.WeatherItem;
import lombok.extern.java.Log;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @program: refresh
 * @description:
 * @author: lvyq
 * @create: 2023-03-15 16:48
 **/
@Log
public class WeatherUtils {
 

     /** 
     * @Description: 获取天气
     * @params:  * @param city
     * @return: 
     * @Author: lvyq
     * @Date: 2023/3/16 14:05
     */
    public static WeatherItem getWeater(ApiInfo apiInfo, String city) throws IOException {
        WeatherItem weatherItem =new WeatherItem();
        String result=sendGet(apiInfo.getUrl()+city+"/");
        Document doc = Jsoup.parse(result);

        String name = doc.getElementsByTag("h1").text();
        String time=doc.getElementsByClass("week").get(0).text();
        String nowWeather= doc.getElementsByClass("now").text();
        weatherItem.setWeatherName(name);
        weatherItem.setTime(time);
        weatherItem.setNowWeather(nowWeather);

        // 今天天气
        String todayWeatherString  = doc.body().getElementsByClass("weather").toString();
        Document docToday = Jsoup.parse(todayWeatherString);
        String todayWeatherTQ=docToday.getElementsByTag("b").get(1).text();
        String todayWeatherWD=docToday.getElementsByTag("span").text();
        todayWeatherWD=todayWeatherWD.replace(todayWeatherTQ,"");
        weatherItem.setTodayWeatherTQ(todayWeatherTQ);
        weatherItem.setTodayWeatherWD(todayWeatherWD);

        //湿度
        String shiduResult= doc.body().getElementsByClass("shidu").toString();
        Document shiduDoc = Jsoup.parse(shiduResult);
        String shidu=shiduDoc.getElementsByTag("b").get(0).text();
        String fengxiang=shiduDoc.getElementsByTag("b").get(1).text();
        String ziwaixian=shiduDoc.getElementsByTag("b").get(2).text();
        weatherItem.setShidu(shidu);
        weatherItem.setFengxiang(fengxiang);
        weatherItem.setZiwaixian(ziwaixian);

         //空气质量
        String kongqiResult= doc.body().getElementsByClass("kongqi").toString();
        Document kongqiDoc = Jsoup.parse(kongqiResult);
        String kongqiTitle = kongqiDoc.getElementsByTag("h5").text();
        String pm= kongqiDoc.getElementsByTag("h6").text();

        String kongqiOth = kongqiDoc.getElementsByTag("span").text();
        weatherItem.setKongqiTitle(kongqiTitle);
        weatherItem.setPm(pm);
        weatherItem.setKongqiOth(kongqiOth);
        return weatherItem;
    }




    public static String sendGet(String url) {
        String result = "";
        BufferedReader in = null;

        try {
            String urlNameString = url;
            System.out.println(urlNameString);
            java.net.URL realUrl = new URL(urlNameString);
            URLConnection connection = realUrl.openConnection();
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.connect();
            Map<String, List<String>> map = connection.getHeaderFields();
            Iterator var8 = map.keySet().iterator();

            while(var8.hasNext()) {
                String key = (String)var8.next();
                System.out.println(key + "--->" + map.get(key));
            }

            String line;
            for(in = new BufferedReader(new InputStreamReader(connection.getInputStream())); (line = in.readLine()) != null; result = result + line) {
            }
        } catch (Exception var18) {
            System.out.println("发送GET请求出现异常！" + var18);
            var18.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception var17) {
                var17.printStackTrace();
            }

        }

        return result;
    }


}
